import { defineStore } from 'pinia';
import { Application } from './application-repository';

export const STORE_KEY_SYSTEM = 'store-system';

export type System = {
  id?: string;
  name: string;
  description: string;
  applications?: Array<Application>;
};

export const useSystemRepository = defineStore(
  STORE_KEY_SYSTEM,
  () => {
    const systems = ref<Array<System>>([
      {
        id: '8f12a674-82ac-4c8d-ae90-a70a068accf9',
        name: '工作流平台',
        description: '提供流程生命周期管理的平台级服务',
      },
      {
        id: 'b486459c-30eb-4af0-8626-a95959667d51',
        name: '文档平台',
        description: '提供文档管理的平台级服务',
      },
    ]);

    const systemContainer = computed(() => {
      const systemContainer_ = new Map<string, System>();
      systems.value.forEach(system => {
        systemContainer_.set(system.id!, system);
      });
      return systemContainer_;
    });

    const addSystem = (system: System) => {
      systems.value = [system].concat(systems.value);
    };

    const getSystem = (systemId: string) => {
      return systemContainer.value.get(systemId);
    };

    return { systems, addSystem, getSystem };
  },
  {
    persist: {
      key: STORE_KEY_SYSTEM,
      storage: localStorage,
    },
  },
);
